import { getAuthHeader } from '@/utils/auth'
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'

const routes: Readonly<RouteRecordRaw[]> = [
  {
    path: '/',
    redirect: '/workbench'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/pages/login/index.vue')
  },
  {
    path: '/workbench',
    name: 'workbench',
    component: () => import('@/pages/workbench/index.vue'),
    children: [
      {
        path: '',
        name: 'desk',
        component: () => import('@/pages/workbench/desk.vue')
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('@/pages/order/index.vue')
      },
      {
        path: 'employee',
        name: 'employee',
        component: () => import('@/pages/employee/index.vue')
      },
      {
        path: 'category',
        name: 'category',
        component: () => import('@/pages/category/index.vue')
      },
      {
        path: 'dish',
        name: 'dish',
        component: () => import('@/pages/dish/index.vue')
      },
      {
        path: 'setmeal',
        name: 'setmeal',
        component: () => import('@/pages/setmeal/index.vue')
      },
      {
        path: 'data-count',
        name: 'data-count',
        component: () => import('@/pages/data-count/index.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory('/'),
  routes
})

router.beforeEach(to => {
  if (to.name !== 'login') {
    const authToken = getAuthHeader()
    if (!authToken) {
      return { name: 'login' }
    }
  }
  return true
})

export default router
